package com.py.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.apis.wemedia.IUserRealNameClient;
import com.py.common.constants.UserConstants;
import com.py.model.common.dtos.PageResponseResult;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.common.enums.AppHttpCodeEnum;
import com.py.model.user.dtos.AuthDto;
import com.py.model.user.pojos.ApUser;
import com.py.model.user.pojos.ApUserRealname;
import com.py.user.mapper.ApUserRealnameMapper;

import com.py.user.service.ApUserRealnameService;
import com.py.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {



    @Resource
    private IUserRealNameClient userRealNameClient;

    @Autowired
    private ApUserService userService;


    /**
     * 查询申请审核的用户
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(AuthDto dto) {

        //校验参数
        dto.checkParam();

        IPage page = new Page(dto.getPage(),dto.getSize());


        //根据状态查询  0创建中 1待审核 2审核失败 9审核通过
        if (dto.getStatus()!=null){


            //根据状态构造查询条件
            LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ApUserRealname::getStatus, dto.getStatus());

            //分页展示
            page(page,lqw);
            PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
            result.setData(page.getRecords());

            return result;
        }

        //查询所有 分页展示
        page(page);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 审核驳回
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {

        //校验参数
        dto.checkParam();
        //根据id更改状态  0创建中 1待审核 2审核失败 9审核通过
        if (dto.getId()!=null){
            //根据id获取对象
            ApUserRealname userRealname = getById(dto.getId());
            //更改状态为审核失败
            userRealname.setStatus(UserConstants.UNAUTH);
            userRealname.setReason(dto.getMsg());

            updateById(userRealname);

            return ResponseResult.okResult(userRealname.getReason());
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }


    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //校验参数
        dto.checkParam();
        //根据id更改状态  0创建中 1待审核 2审核失败 9审核通过
        if (dto.getId()!=null){
            //根据id获取对象
            ApUserRealname userRealname = getById(dto.getId());

            //更改状态为审核通过
            userRealname.setStatus(UserConstants.AUTH);

            boolean updateById = updateById(userRealname);

            ApUser apUser = userService.getById(userRealname.getUserId());

            if (updateById){
                //feign 调用 userRealNameClient修改user表格
                userRealNameClient.savePassUser(apUser);

            }else {

                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            return ResponseResult.okResult(null);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }


}
